import { getSysInfo, setSysInfo, getSysCfg, setSysCfg, getPortStatistics, upload,
  exportLog, deleteLog, getLogLevel, setLogLevel, getLogFile, upgradeSoftware, clearPortStatistics, applyAndReboot } from '@/api/device'

const getDefaultState = () => {
  return {
    SysCfgInfo: {
      eth: [],
      notice: {
        countTime: 10,
        infoTime: 10,
        serverIp: '192.168.1.10',
        serverPort: 11000
      },
      uart: [],
      uart2uartRule: [],
      uart2udpRule: [],
      udp2uartRule: {
        multicast: [],
        udp2uart: []
      }
    }
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_SYS_CFG: (state, SysCfgInfo) => {
    state.SysCfgInfo = SysCfgInfo
  },
  SET_multicast: (state) => {
    if (Array.isArray(state.SysCfgInfo.udp2uartRule.multicast)) {
      state.SysCfgInfo.udp2uartRule.multicast = state.SysCfgInfo.udp2uartRule.multicast.map((item, index) => {
        return {
          id: index + 1,
          ...item
        }
      })
    } else {
      console.error('multicast is not an array')
    }
  }
}

const actions = {
  // user login
  getSysInfo() {
    return new Promise((resolve, reject) => {
      getSysInfo().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getPortStatistics() {
    return new Promise((resolve, reject) => {
      getPortStatistics().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  clearPortStatistics() {
    return new Promise((resolve, reject) => {
      clearPortStatistics().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  setSysInfo({ commit }, sysInfo) {
    return new Promise((resolve, reject) => {
      setSysInfo(sysInfo).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getSysCfg() {
    return new Promise((resolve, reject) => {
      getSysCfg().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  setSysCfg({ commit }, sysInfo) {
    return new Promise((resolve, reject) => {
      setSysCfg(sysInfo).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  applyAndReboot({ commit }, sysInfo) {
    return new Promise((resolve, reject) => {
      applyAndReboot(sysInfo).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  upload({ commit }, file) {
    return new Promise((resolve, reject) => {
      upload(file).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  upgradeSoftware({ commit }, file) {
    return new Promise((resolve, reject) => {
      upgradeSoftware(file).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getLogLevel() {
    return new Promise((resolve, reject) => {
      getLogLevel().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  setLogLevel({ commit }, logInfo) {
    return new Promise((resolve, reject) => {
      setLogLevel(logInfo).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  exportLog() {
    return new Promise((resolve, reject) => {
      exportLog().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getLogFile() {
    return new Promise((resolve, reject) => {
      getLogFile().then(response => {
        console.log(response)
        resolve(response)
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  },

  deleteLog() {
    return new Promise((resolve, reject) => {
      deleteLog().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

